<?php
//创建画布
session_start();
$num    = 4;
$width  = $num * 100;
$height = 100;
$img    = imagecreatetruecolor($width, $height);
//画布颜色
$bgc = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $bgc);
//验证码  
$str   = 'QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm';  //验证码随机出现的字符串
$yzm = '';
//随机循环刷新四个验证码
for ($i = 1; $i <= $num; $i++) {
    $reg = $str[mt_rand(0, 61)];
    $color = imagecolorallocate($img, mt_rand(0, 123), mt_rand(0, 123), mt_rand(0, 123));    //验证码字体颜色
    imagettftext($img, 50, mt_rand(-20, 20),  30+ ($i - 1) * 100, $height * 3 / 4, $color, 'ccxkt.TTF', $reg);
    $yzm .= $reg;
    $_SESSION['yzm']=$yzm;
}
// 随机点
for ($i=0; $i < 500; $i++) { 
$color1 = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $color1);
}
//随机线条
for ($i=0; $i < 20; $i++) { 
$color2 = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$height), mt_rand(0,$width), $color2);
}
//输出图像
header("Content-Type: image/png");
imagepng($img);
//销毁图像
imagedestroy($img);
?>